package me.shibor.study.quartz.demo;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;

import me.shibor.study.quartz.job.HelloExpJob;
import me.shibor.study.quartz.listener.HelloJobListener;

public class JobListenerExample {

	public static void main(String[] args) throws SchedulerException {
		
		JobKey jobKey = new JobKey("dummyJobName", "group1");
		JobDetail job = JobBuilder.newJob(HelloExpJob.class).withIdentity(jobKey).build();

		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName", "group1")
				.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();

		Scheduler scheduler = new StdSchedulerFactory().getScheduler();

		// Listener attached to jobKey
		scheduler.getListenerManager().addJobListener(new HelloJobListener(), KeyMatcher.keyEquals(jobKey));

		// Listener attached to group named "group 1" only.
		// scheduler.getListenerManager().addJobListener(
		// new HelloJobListener(), GroupMatcher.jobGroupEquals("group1")
		// );

		scheduler.start();
		scheduler.scheduleJob(job, trigger);

	}

}
